act_site = [1 2 4 7 8];
pas_site = setdiff(1:9,act_site);
con_site = 10;

for site = 1:9
    for time = 1:4
        for layer = 1:4
            cur_act = proj_meta(site).rd(layer,time).act;
            cur_act_f = [];
            for cell_id = 1:size(cur_act,1)
                cur_cact = cur_act(cell_id,:);
                cur_cact(cur_cact <= crit{site,layer,cell_id}) = 1;
                cur_act_f(cell_id,:) = cur_cact;
            end
            
            % generate binary activity of motion.
            velM_bi = -proj_meta(site).rd(layer,time).velM_smoothed;
            velM_bi(velM_bi > 0.001) = 1;
            velM_bi(velM_bi <= 0.001) = 0;
            
            % generate binary activity of visual flow.
            velP_bi = -proj_meta(site).rd(layer,time).velP_smoothed;
            velP_bi(velP_bi > 0.001) = 1;
            velP_bi(velP_bi <= 0.001) = 0;
            
            VM_bi = vertcat(velP_bi,velM_bi);
            
            bl_id = 0;
            fb_id = 0;
            pb_id = 0;
            mm_id = 0;
            bl = [];
            fb = [];
            pb = [];
            mm = [];
            for frame_id = 1:size(VM_bi,2)
                if VM_bi(:,frame_id) == [0;0]
                    bl_id = bl_id + 1;
                    bl(bl_id) = frame_id; % frame_id of baseline
                end
                if VM_bi(:,frame_id) == [1;1]
                    fb_id = fb_id + 1;
                    fb(fb_id) = frame_id; % frame_id of feedback
                end
                if VM_bi(:,frame_id) == [1;0]
                    pb_id = pb_id + 1;
                    pb(pb_id) = frame_id; % frame_id of playback
                end
                if VM_bi(:,frame_id) == [0;1]
%                     if frame_id <= 7493
                    mm_id = mm_id + 1;
                    mm(mm_id) = frame_id; % frame_id of mismatch
%                     end
                end
            end
            
            bl_act = [];
            fb_act = [];
            pb_act = [];
            mm_act = [];
            for cell_id = 1:size(cur_act_f,1)
                bl_act(cell_id,:) = cur_act_f(cell_id,bl);
                fb_act(cell_id,:) = cur_act_f(cell_id,fb);
                pb_act(cell_id,:) = cur_act_f(cell_id,pb);
                mm_act(cell_id,:) = cur_act_f(cell_id,mm);
            end
            yas{site}(layer,time) = struct('bl',bl_act,'fb',fb_act,'pb',pb_act,'mm',mm_act);
        end
    end
end